﻿using Microsoft.AspNetCore.Mvc;
using Elasticsearch.Service;
using Elasticsearch.Model;
using Elastic.Clients.Elasticsearch;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Elasticsearch.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class IndexController : ControllerBase
    {
        public IIndexService IndexService;

        public IUserService UserService;

        public IndexController(IIndexService indexService, IUserService userService)
        {
            IndexService = indexService;
            UserService = userService;
        }

        [Route("CreateIndex")]
        [HttpGet]
        public void CreateIndex()
        {
            IndexService.CreateUserIndexAsync();
        }

        [Route("CreateUser")]
        [HttpGet]
        public async void CreateUser(string RealName)
        {
            var result = new User()
            {
                Id = Guid.NewGuid().ToString(),
                Username = "Jack",
                Email = "jack@example.com",
                Phone = "12345678901",
                RealName = RealName,
                Age = 25,
                Gender = 1,
                Address = RealName,
                Status = 1,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                Tags = new List<string> { "管理员", "VIP" },
                Settings = new Dictionary<string, object> { { "theme", "dark" } }
            };
            await UserService.AddUserAsync(result);
        }

        [Route("SearchUsersAsync")]
        [HttpGet]
        public async Task<List<User>> SearchUsersAsync(string RealName)
        {
            return await UserService.SearchUsersAsync(RealName);
        }


        [Route("SerachAgretetion")]
        [HttpGet]
        public async Task<object> SerachAgretetion(string RealName)
        {
            return await UserService.SerachAgretetion();
        }

        [Route("AutocompletionAddData")]
        [HttpGet]
        public async Task<bool?> AutocompletionAddData()
        {
            return await UserService.AutocompletionAddData();
        }

        [Route("GetAutocompletionData")]
        [HttpGet]
        public async Task<bool?> GetAutocompletionData()
        {
            return await UserService.GetAutocompletionData();
        }

        // GET: api/<IndexController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<IndexController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<IndexController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<IndexController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<IndexController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {

        }
    }
}
